package com.xinchuang.mapper.targetSystem.weapon;

import com.xinchuang.entity.targetSystem.weapon.WeaponAttribute;
import com.xinchuang.entity.targetSystem.weapon.WeaponMessage;
import com.xinchuang.entity.targetSystem.weapon.WeaponRelation;
import com.xinchuang.entity.targetSystem.weapon.WeaponType;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @ClassName WeaponHouseMapper
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-08-17 15:02
 * @Verion V1.0
 */
@Repository
public interface WeaponHouseMapper {

    //武器库类型数据库操作模块
    @Select(value = "SELECT type_id,parent_id,type_name,level FROM weapon_type ORDER BY create_time ASC")
    @Results(id = "typeResultMap", value = {
            @Result(property = "typeId", column = "type_id"),
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "typeName", column = "type_name"),
            @Result(property = "level", column = "level")
    })
    List<WeaponType> findTreeAll();

    @Select(value = "SELECT type_id,parent_id,type_name,level FROM weapon_type WHERE parent_id = #{parentId}")
    @ResultMap(value = "typeResultMap")
    List<WeaponType> findTreeAllByParentId(String parentId);

    @Insert(value = {
        "INSERT INTO weapon_type ",
        "(type_id,parent_id,type_name,level,sort) ",
        "VALUES ",
        "(#{typeId},#{parentId},#{typeName},#{level},#{sort})"
    })
    int addType(WeaponType weaponType);

    @Update(value = {
            "UPDATE weapon_type SET type_name = #{typeName} WHERE type_id = #{typeId}"
    })
    int updateType(WeaponType weaponType);

    @Delete(value = "DELETE FROM weapon_type WHERE type_id = #{type_id}")
    int delType(@Param("type_id") String typeId);

    //武器库类型连接数据数据库操作
    @Select(value = {
            "SELECT id,key_name,key,key_type,show_type,required,info,data_range,verify,min,max,enumerator,row,message,sort ",
            "FROM weapon_attribute ",
            "WHERE type_id = #{typeId} ORDER BY sort ASC"
    })
    @Results(id = "weaponResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "typeId", column = "type_id"),
            @Result(property = "keyName", column = "key_name"),
            @Result(property = "key", column = "key"),
            @Result(property = "keyType", column = "key_type"),
            @Result(property = "showType", column = "show_type"),
            @Result(property = "required", column = "required"),
            @Result(property = "info", column = "info"),
            @Result(property = "dataRange", column = "data_range"),
            @Result(property = "verify", column = "verify"),
            @Result(property = "min", column = "min"),
            @Result(property = "max", column = "max"),
            @Result(property = "enumerator", column = "enumerator"),
            @Result(property = "row", column = "row"),
            @Result(property = "message", column = "message"),
            @Result(property = "sort", column = "sort")
    })
    List<WeaponAttribute> findWeaponByTypeId(@Param("typeId") String typeId);

    @Insert(value = {
            "INSERT INTO weapon_attribute ",
            "(id,type_id,key_name,key,key_type,show_type,required,info,data_range,verify,min,max,enumerator,row,message,sort) ",
            "VALUES ",
            "(#{id},#{typeId},#{keyName},#{key},#{keyType},#{showType},#{required},#{info},#{dataRange},#{verify},#{min},#{max},#{enumerator},#{row},#{message},#{sort})"
    })
    int addWeapon(WeaponAttribute weaponAttribute);

    @Update(value = {
            "<script>",
            "UPDATE weapon_attribute ",
            "<set>",
            "<if test = 'keyName != null'>key_name = #{keyName},</if>",
            "<if test = 'key != null'>key = #{key},</if>",
            "<if test = 'keyType != null'>key_type = #{keyType},</if>",
            "<if test = 'showType != null'>show_type = #{showType},</if>",
            "<if test = 'required != null'>required = #{required},</if>",
            "<if test = 'info != null'>info = #{info},</if>",
            "<if test = 'dataRange != null'>data_range = #{dataRange},</if>",
            "<if test = 'verify != null'>verify = #{verify},</if>",
            "<if test = 'min != null'>min = #{min},</if>",
            "<if test = 'max != null'>max = #{max},</if>",
            "<if test = 'enumerator != null'>enumerator = #{enumerator},</if>",
            "<if test = 'row != null'>row = #{row},</if>",
            "<if test = 'message != null'>message = #{message},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateWeapon(WeaponAttribute weaponAttribute);

    @Delete(value = "DELETE FROM weapon_attribute WHERE id = #{id}")
    int delWeapon(@Param("id") String id);

    @Delete(value = "DELETE FROM weapon_attribute WHERE relation_id = #{id}")
    int delWeaponAttributeByRelationId(String id);

    @Select(value = {
            "SELECT id,type_id,sort ",
            "FROM weapon_attribute WHERE id = #{id} LIMIT 1"
    })
    @ResultMap(value = "weaponResultMap")
    WeaponAttribute findWeaponById(@Param("id") String id);

    @Select(value = {
            "SELECT id FROM weapon_attribute WHERE type_id = #{typeId} ORDER BY sort ASC"
    })
    List<String> findWeaponSort(@Param("typeId") String typeId);

    @Update(value = "UPDATE weapon_attribute SET sort = #{sort} WHERE id = #{id}")
    int updateWeaponSort(@Param("id") String id, @Param("sort") Integer sort);

    @Select("SELECT MAX(sort) FROM weapon_attribute WHERE type_id = #{typeId}")
    Integer getWeaponMaxSort(@Param("typeId") String typeId);

    @Select("SELECT id,MIN(sort) AS value FROM weapon_attribute WHERE type_id = #{typeId}")
    Map<String, Object> getMin(@Param("typeId") String typeId);

    @Select("SELECT id,MAX(sort) AS value FROM weapon_attribute WHERE type_id = #{typeId}")
    Map<String, Object> getMax(@Param("typeId") String typeId);

    @Select("SELECT id,sort FROM weapon_attribute WHERE sort < #{sort} AND type_id = #{typeId} ORDER BY sort DESC LIMIT 1")
    Map<String, Object> getUp(@Param("sort") int sort, @Param("typeId") String typeId);

    @Select("SELECT id,sort FROM weapon_attribute WHERE sort > #{sort} AND type_id = #{typeId} ORDER BY sort ASC LIMIT 1")
    Map<String, Object> getDown(@Param("sort") int sort, @Param("typeId") String typeId);

    //武器库信息录入数据库操作模块
    @Select(value = {
            "SELECT id,key,key_name,key_type,key_value,remark,person,source FROM weapon_message WHERE relation_id = #{relationId}"
    })
    @Results(value = {
            @Result(property = "keyName", column = "key_name"),
            @Result(property = "keyType", column = "key_type"),
            @Result(property = "keyValue", column = "key_value")
    })
    List<WeaponMessage> selectWeaponMessageAll(String relationId);

    @Select(value = {
            "SELECT id,key,key_name,key_type,key_value,remark,person,source FROM weapon_message WHERE relation_id = (SELECT weapon_id FROM weapon_target_relation WHERE id = #{id}) AND key = #{key} LIMIT 1"
    })
    @Results(value = {
            @Result(property = "keyName", column = "key_name"),
            @Result(property = "keyType", column = "key_type"),
            @Result(property = "keyValue", column = "key_value")
    })
    WeaponMessage selectWeaponMessageByIdAndKey(String id, String key);

    @Insert(value = {
            "<script>",
            "INSERT INTO weapon_message ",
            "(id,relation_id,key,key_name,key_type,key_value,remark,person,source) ",
            "VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id},#{item.relationId},#{item.key},#{item.keyName},#{item.keyType},#{item.keyValue},#{item.remark},#{item.person},#{item.source})",
            "</foreach>",
            "</script>"
    })
    int addOrUpdateWeaponMessageAll(List<WeaponMessage> list);

    @Delete(value = "DELETE FROM weapon_message WHERE relation_id = #{id}")
    int delWeaponMessageByRelationId(String id);

    //武器库与目标绑定数据库操作
    @Insert(value = {
            "INSERT INTO weapon_target_relation ",
            "(id, target_id, type_id, weapon_id, position, icon, remark) ",
            "VALUES ",
            "(#{id},#{targetId},#{typeId},#{weaponId},#{position},#{icon},#{remark})"
    })
    int addWeaponTargetRelation(WeaponRelation weaponRelation);

    @Update(value = {
            "<script>",
            "UPDATE weapon_target_relation ",
            "<set>",
            "<if test = 'typeId != null'>type_id = #{typeId},</if>",
            "<if test = 'weaponId != null'>weapon_id = #{weaponId},</if>",
            "<if test = 'position != null'>position = #{position},</if>",
            "<if test = 'icon != null'>icon = #{icon},</if>",
            "<if test = 'remark != null'>remark = #{remark},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateWeaponTargetRelation(WeaponRelation weaponRelation);

    @Delete(value = "DELETE FROM weapon_target_relation WHERE id = #{id}")
    int delWeaponTargetRelation(String id);

    @Delete(value = "DELETE FROM weapon_target_relation WHERE type_id = #{typeId}")
    int delWeaponTargetRelationByTypeId(String typeId);

    @Delete(value = "DELETE FROM weapon_target_relation WHERE weapon_id = #{weaponId}")
    int delWeaponTargetRelationByWeaponId(String weaponId);

    @Select(value = "SELECT id,type_id,weapon_id,position,icon,remark FROM weapon_target_relation WHERE id = #{id}")
    @Results(value = {
            @Result(property = "typeId", column = "type_id"),
            @Result(property = "weaponId", column = "weapon_id")
    }, id = "relationResultMap")
    WeaponRelation selectWeaponTargetRelationOnly(String id);

    @Select(value = "SELECT id,type_id,weapon_id,position,icon,remark FROM weapon_target_relation WHERE target_id = #{targetId}")
    @ResultMap(value = "relationResultMap")
    List<WeaponRelation> selectWeaponTargetRelationByTargetId(String targetId);

    @Select(value = "SELECT id,type_id,weapon_id,position,icon,remark FROM weapon_target_relation WHERE type_id = #{typeId}")
    @ResultMap(value = "relationResultMap")
    List<WeaponRelation> selectWeaponTargetRelationByTypeId(String typeId);

    @Select(value = "SELECT id,type_id,weapon_id,position,icon,remark FROM weapon_target_relation WHERE weapon_id = #{weaponId}")
    @ResultMap(value = "relationResultMap")
    List<WeaponRelation> selectWeaponTargetRelationByWeaponId(String weaponId);

    @Select(value = {
            "SELECT wr.id,wr.target_id,wt.type_name AS type_id,st.type_name AS weapon_id,wr.position,wr.icon,wr.remark ",
            "FROM weapon_target_relation wr ",
            "LEFT JOIN weapon_type wt ON wr.type_id = wt.type_id ",
            "LEFT JOIN system_type st ON wr.weapon_id = st.id ",
            "WHERE wr.target_id = #{targetId}"
    })
    @ResultMap(value = "relationResultMap")
    List<WeaponRelation> selectWeaponTargetRelationNameByTargetId(String targetId);

    //武器分析
    @Select(value = "SELECT wtr.type_id,wt.type_name AS weapon_id FROM weapon_target_relation wtr JOIN weapon_type wt ON wtr.type_id = wt.type_id WHERE wtr.target_id = '975804826866814976' GROUP BY wtr.type_id")
    @ResultMap(value = "relationResultMap")
    List<WeaponRelation> selectTargetTypeId(String targetId);

    @Select(value = "SELECT wtr.id,wtr.type_id,st.type_name AS weapon_id FROM weapon_target_relation wtr LEFT JOIN system_type st ON wtr.weapon_id = st.id WHERE wtr.target_id = #{targetId}")
    @ResultMap(value = "relationResultMap")
    List<WeaponRelation> selectTargetWeaponId(String targetId);

}
